/*
 *MIT License
 *
 *Copyright (c) 2019 chenshuai cs4380@163.com
 *
 *Permission is hereby granted, free of charge, to any person obtaining a copy
 *of this software and associated documentation files (the "Software"), to deal
 *in the Software without restriction, including without limitation the rights
 *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *copies of the Software, and to permit persons to whom the Software is
 *furnished to do so, subject to the following conditions:
 *
 *The above copyright notice and this permission notice shall be included in all
 *copies or substantial portions of the Software.
 *
 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 *SOFTWARE.
 */

package com.cs.cslc.auth.rest;

import com.cs.cslc.auth.service.UserService;
import com.cs.cslc.auth.vo.LoginUserVO;
import com.cs.cslc.auth.vo.TokenVO;
import com.cs.cslc.common.constant.HttpStatusConstant;
import com.cs.cslc.common.constant.MessageConstant;
import com.cs.cslc.common.msg.ObjectResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * AuthController 认证授权.
 * <p>
 * 登陆，登出
 * </p>
 *
 * @author cs
 * @version 1.0
 * @date 2019-05-06 19:46
 * @description
 */
@RestController
@RequestMapping("/auth")
@Api(tags = "认证授权")
public class AuthController {

    @Autowired
    private UserService userService;

    /**
     * 用户登陆，获取token信息
     *
     * @param loginUserVO 登陆信息
     * @return token信息
     */
    @PostMapping("/login")
    @ApiOperation("用户登陆，获取token信息")
    public ObjectResultResponse<TokenVO> login(@RequestBody @Validated LoginUserVO loginUserVO, BindingResult bindingResult) {
        ObjectResultResponse<TokenVO> result = new ObjectResultResponse<>();
        // 判单验证
        if (bindingResult.hasErrors()) {
            result.setStatus(HttpStatusConstant.FAIL);
            result.setMessage(MessageConstant.MVC_VALIDATE_MSG);
            return result;
        }
        TokenVO tokenVO = new TokenVO();
        tokenVO.setToken(userService.login(loginUserVO.getAccount().trim(), loginUserVO.getPassword().trim()));
        result.setData(tokenVO);
        return result;
    }

    /**
     * 登出当前用户
     *
     * @return
     */
    @PostMapping("/logout")
    public ObjectResultResponse<String> logout() {
        ObjectResultResponse res = new ObjectResultResponse();
        // TODO 清楚缓存token
        return res;
    }
}